<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item> <i class="el-icon-lx-cascades"></i> 基础表格 </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="handle-box">
                <el-button type="primary" icon="el-icon-delete" class="handle-del mr10" @click="delAllSelection">批量删除</el-button>
                <!-- <el-select v-model="query.address" placeholder="地址" class="handle-select mr10">
                    <el-option key="1" label="广东省" value="广东省"></el-option>
                    <el-option key="2" label="湖南省" value="湖南省"></el-option>
                </el-select> -->
                <el-input v-model="query.username" placeholder="用户名" class="handle-input mr10"></el-input>
                <el-button type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
                <el-button type="primary" icon="el-icon-circle-plus" @click="handleAdd">添加用户</el-button>
            </div>
            <el-table
                :data="tableData"
                border
                class="table"
                ref="multipleTable"
                header-cell-class-name="table-header"
                @selection-change="handleSelectionChange"
            >
                <el-table-column type="selection" width="55" align="center"></el-table-column>
                <el-table-column prop="user_id" label="ID" width="80" align="center"></el-table-column>
                <el-table-column prop="username" label="用户账号"></el-table-column>
                <el-table-column prop="shop_name" label="超市名称"></el-table-column>
                <el-table-column prop="shop_manager" label="超市负责人"></el-table-column>
                <el-table-column prop="contact" label="联系电话" width="120"></el-table-column>
                <el-table-column prop="create_time" width="100" label="创建时间"></el-table-column>
                <el-table-column prop="last_logintime" label="最后登录时间" width="100"></el-table-column>
                <el-table-column label="操作" width="180" align="center">
                    <template slot-scope="scope">
                        <el-button type="text" icon="el-icon-edit" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                        <el-button type="text" icon="el-icon-delete" class="red" @click="handleDelete(scope.$index, scope.row)"
                            >删除</el-button
                        >
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination
                    background
                    layout="total, prev, pager, next"
                    :current-page="query.currentPage"
                    :page-size="query.pageSize"
                    :page-count="pageTotal"
                    @current-change="handlePageChange"
                ></el-pagination>
            </div>
        </div>

        <!-- 编辑弹出框 -->
        <el-dialog title="编辑" :visible.sync="editVisible" width="30%">
            <el-form ref="form" :model="form" label-width="70px">
                <el-form-item label="账号名">
                    <label>{{ form.username }}</label>
                </el-form-item>
                <el-form-item label="重置密码">
                    <el-button type="primary" @click="resetPass(form.user_id)">重置密码</el-button><label class="mg-left">{{ resetPassword }}</label>
                </el-form-item>
                <el-form-item label="账号角色">
                    <el-select v-model="form.user_role" placeholder="选择角色">
                        <el-option label="普通用户" value="普通用户"></el-option>
                        <el-option label="管理员" value="管理员"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="超市名称">
                    <el-input v-model="form.shop_name"></el-input>
                </el-form-item>
                <el-form-item label="超市负责人">
                    <el-input v-model="form.shop_manager"></el-input>
                </el-form-item>
                <el-form-item label="联系方式">
                    <el-input v-model="form.contact"></el-input>
                </el-form-item>
                <el-form-item label="超市地址">
                    <el-input v-model="form.shop_address"></el-input>
                </el-form-item>
                <el-form-item label="所在城市">
                    <el-input v-model="form.shop_city"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="editVisible = false">取 消</el-button>
                <el-button type="primary" @click="saveEdit">确 定</el-button>
            </span>
        </el-dialog>

        <!-- 添加弹出框 -->
        <el-dialog title="添加产品" :visible.sync="addVisible" width="35%">
            <el-form ref="form" :model="form" label-width="90px">
                <el-form-item label="账号名">
                    <el-input v-model="form.username"></el-input>
                </el-form-item>
                <el-form-item label="账号密码">
                    <el-input type="password" v-model="form.user_password"></el-input>
                </el-form-item>
                <el-form-item label="账号角色">
                    <el-select v-model="form.user_role" placeholder="选择角色">
                        <el-option label="普通用户" value="普通用户"></el-option>
                        <el-option label="管理员" value="管理员"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="超市名称">
                    <el-input v-model="form.shop_name"></el-input>
                </el-form-item>
                <el-form-item label="超市负责人">
                    <el-input v-model="form.shop_manager"></el-input>
                </el-form-item>
                <el-form-item label="联系方式">
                    <el-input v-model="form.contact"></el-input>
                </el-form-item>
                <el-form-item label="超市地址">
                    <el-input v-model="form.shop_address"></el-input>
                </el-form-item>
                <el-form-item label="所在城市">
                    <el-input v-model="form.shop_city"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="addVisible = false">取 消</el-button>
                <el-button type="primary" @click="saveAdd">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
// import { fetchData } from '../../api/index';

export default {
    name: 'ClientTable',
    data() {
        return {
            query: {
                username: '',
                currentPage: 1,
                pageSize: 10
            },
            tableData: [],
            multipleSelection: [], //多选框数组
            delList: [], //批量删除
            editVisible: false,
            addVisible: false,
            vilidateBarcodeMessage: '',
            pageTotal: 0,
            form: {
                user_password:'123456'
            },
            idx: -1, //当前分页页面的第几行数据
            id: -1 ,//
            resetPassword:""
        };
    },
    components: {
        //引入外部组件，这里是定义
    },
    created() {
        this.InitData();
    },
    methods: {
        // 获取 easy-mock 的模拟数据
        InitData() {
            this.getData();
        },
        async getData() {
            let res = await this.$http.post('getUsersByPager', this.query);
            this.tableData = res.data.data;
            this.pageTotal = res.data.pageTotal;            
        },
        // 触发搜索按钮
        handleSearch() {
            //this.$set(this.query, 'pageIndex', 1);
            this.InitData();
        },
        // 删除操作
        handleDelete(index, row) {
            let ids = row.user_id;
            // 二次确认删除
            this.$confirm('确定要删除吗？', '提示', {
                type: 'warning'
            })
                .then(() => {
                    this.saveDelete(ids);
                    this.tableData.splice(index, 1);
                })
                .catch(() => {});

        },
        // 多选操作
        handleSelectionChange(val) {
            this.multipleSelection = val;
        },
        // 删除方法封装
        async saveDelete(ids) {
            let res = await this.$http.post('deleteUser', { ids: ids });
            if (res.data) {
                this.$message.success(`删除了${ids}`);
                this.InitData();
            } else {
                this.$message.error(`删除了${ids}`);
            }
        },
        // 批量删除
        delAllSelection() {
            const length = this.multipleSelection.length;
            let ids = '';
            //this.delList = this.delList.concat(this.multipleSelection);
            for (let i = 0; i < length; i++) {
                ids += this.multipleSelection[i].user_id + ',';
            }
            ids = ids.substring(0, ids.length - 1);

            this.$confirm('确定要删除吗？', '提示', {
                type: 'warning'
            })
                .then(() => {
                    //执行删除
                    this.saveDelete(ids);
                })
                .catch(() => {});
        },
        // 编辑操作
        handleEdit(index, row) {
            this.form = {};
            this.idx = index;
            this.form = row;
            this.editVisible = true;
        },
        cancelAdd() {
            this.editVisible = false;
            this.destroyed();
        },
        // 保存编辑
        async saveEdit() {
            this.editVisible = false;
            let res = await this.$http.post('editUser', this.form);
            if (res.data) {
                this.$message.success(`修改第 ${this.idx + 1} 行成功`);
                this.InitData();
            } else {
                this.$message.error(`修改第 ${this.idx + 1} 行失败`);
            }

            //console.log(this.form);
            //this.$set(this.tableData, this.idx, this.form);
        },
        async resetPass(uid){
            this.resetPassword = "";
            //重置密码
            let res = await this.$http.post('resetUserPass', {user_id:uid});
            if (res.state == 200) {
                this.resetPassword = "新密码为：" + res.data;
                this.$message.success(res.message);
            } else {
                this.$message.error(res.message);
            }            
        },
        //添加操作
        handleAdd() {
            this.form = {};
            this.addVisible = true;
        },
        //保存添加
        async saveAdd() {
            this.addVisible = false;
            let res = await this.$http.post('addUser', this.form);
            if (res.state==200) {
                this.$message.success(res.message);
                this.InitData();
            } else {
                this.$message.error(res.message);
            }            
        },
        // 分页导航
        handlePageChange(val) {
            // console.log(val);
            // this.$set(this.query, 'currentPage', val);
            this.query.currentPage = val;
            this.getData();
        },        
        //关闭摄像头
        destroyed() {
            if (!this.mediaStreamTrack) {
                return;
            }
            this.mediaStreamTrack.srcObject.getTracks()[0].stop();
            this.trackerTask.stop();
            this.localVideoTrack.setMuted(true);
        }
    }
};
</script>

<style scoped>
.handle-box {
    margin-bottom: 20px;
}

.mg-left{
    margin-left:10px;
}

.handle-select {
    width: 120px;
}

.handle-input {
    width: 300px;
    display: inline-block;
}
.table {
    width: 100%;
    font-size: 14px;
}
.red {
    color: #ff0000;
}
.mr10 {
    margin-right: 10px;
}
.table-td-thumb {
    display: block;
    margin: auto;
    width: 40px;
    height: 40px;
}
</style>
